#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'anki_manage'
from utils import AnkiConnect


class AnkiManage:
    def __init__(self):
        # assert self.is_online(), \
        #     'AnkiConnect is not installed, or Anki app is not open.'  # Does not work with @staticmethod
        pass
    @staticmethod
    def deckNames():
        res = AnkiConnect.post('deckNames')
        return res["result"]

    @staticmethod
    def modelNames():
        res = AnkiConnect.post('modelNames')
        return res["result"]

    # 根据返回的Ture 判断是否创建成功
    @staticmethod
    def addNote(deckName, modelName, fields, tags=[]):
        params = {"notes": [
            {
                "deckName": deckName,
                "modelName": modelName,
                "fields": fields,
                "tags": tags
            }
        ]}
        res = AnkiConnect.post('addNotes', params)
        return res["result"][0] != None
    @staticmethod
    def listNote(deckName):
        noteIds = AnkiConnect.post('findNotes', {
            "query": "deck:"+deckName
        })
        if(len(noteIds["result"])<0):
            return []
        notes = AnkiConnect.post('notesInfo', {
            "notes": noteIds["result"]
        })
        return notes["result"]
    @staticmethod
    def addNoteTags(noteIds,tags):
        if (isinstance(noteIds, list)==False):
            noteIds = [noteIds]
        if (isinstance(tags, list)==False):
            tags = [tags]
        for tag in tags:
            res = AnkiConnect.post('addTags', {
                "notes": noteIds,
                "tags": tag
            })

    @staticmethod
    def removeNoteTags(noteIds, tags):
        if (isinstance(noteIds, list)==False):
            noteIds = [noteIds]
        if (isinstance(tags, list)==False):
            tags = [tags]
        for tag in tags:
            res = AnkiConnect.post('removeTags', {
                "notes": noteIds,
                "tags": tag
            })
    @staticmethod
    def clearUnusedTags():
        AnkiConnect.post('clearUnusedTags')
